package com.wdb007.venus.svc.biz.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatUtil {

	private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss" ;
	
	private static final String DATE_PATTERN = "yyyy-MM-dd";
	
	private static final String TIME_PATTERN = "HH:mm:ss" ;
	
	private static final String DATE_PATTERN2 = "yyyy年MM月dd日";

	private static final String DATETIME_PATTERN_TIME = "yyyy-MM-dd HH:mm";
	
	private static final String DATE_PATTERN3 = "yyyy.MM.dd";

	public static Date parseDate(String date) {
		DateFormat df = new SimpleDateFormat(DATE_PATTERN);
		Date d = null;
		try {
			d = df.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static Date parseDateTime(String datetime) {
		DateFormat df = new SimpleDateFormat(DATETIME_PATTERN);
		Date d = null;
		try {
			d = df.parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static Date parseTime(String time) {
		DateFormat df = new SimpleDateFormat(TIME_PATTERN);
		Date d = null;
		try {
			d = df.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static String formatDate(Date date) {
		DateFormat df = new SimpleDateFormat(DATE_PATTERN);
		return df.format(date);
	}
	
	public static String formatDate2(Date date) {
		DateFormat df = new SimpleDateFormat(DATE_PATTERN2);
		return df.format(date);
	}
	
	public static String formatDate4(Date date) {
		DateFormat df = new SimpleDateFormat(DATE_PATTERN3);
		return df.format(date);
	}
	public static String formatTime(Date date) {
		DateFormat df = new SimpleDateFormat(TIME_PATTERN);
		return df.format(date);
	}
	
	public static String formatDateTime(Date date) {
		DateFormat df = new SimpleDateFormat(DATETIME_PATTERN);
		return df.format(date);
	}
	public static String formatDate3(Date date) {
		DateFormat df = new SimpleDateFormat(DATETIME_PATTERN_TIME);
		return df.format(date);
	}
	
	/**
	 * 
	 * @param date
	 * @param day 想要获取的日期与传入日期的差值 比如想要获取传入日期前四天的日期 day=-4即可
	 * @return
	 */
	public static Date getSomeDay(Date date, int day){
	        Calendar calendar = Calendar.getInstance();
	        calendar.setTime(date);
	        calendar.add(Calendar.DATE, day);
	        return calendar.getTime();
	 }
	/**
	 * 获取传递日期的最大时间即: 23:59:59
	 * @param date
	 * @return
	 */
	public static Date getMaxTimeDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	 /**
	  * 获取两个时间日期的差值
	  * @param startDate
	  * @param endDate
	  * @return
	  */
	 public static int getDays(Date startDate, Date endDate) {
		 if(startDate.equals(endDate)) {
			 return 0;
		 }
		 Long startMillonSeconds = startDate.getTime();
		 Long endMillionSeconds = endDate.getTime();
		 int between_days=(int)((endMillionSeconds-startMillonSeconds)/(1000*3600*24)); 
		 return between_days;
	 }
	 
	 public static void main(String[] args) {
		Date endDate = parseDateTime("2018-03-29 23:59:59");
		Date startDate = new Date();
		System.out.println(getDays(startDate, endDate));
	}
}
